package client;

import dao.BossDAO;
import model.Boss;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * 老板客户端
 */
public class BossClient {
    private static final String SERVER_ADDRESS = "localhost";
    private static final int SERVER_PORT = 8888;
    private static String bossId;
    
    public static void main(String[] args) {
        try (Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
            
            System.out.println("欢迎使用超市管理系统-老板端");
            
            // 登录流程
            if (!login(stdIn)) {
                System.out.println("登录失败，程序退出");
                return;
            }
            
            // 发送角色信息给服务器
            out.println("boss:" + bossId);
            
            // 启动一个线程接收服务器消息
            Thread receiverThread = new Thread(() -> {
                try {
                    String response;
                    while ((response = in.readLine()) != null) {
                        System.out.println("\n服务器消息: " + response);
                        
                        // 处理补货确认消息
                        if (response.startsWith("restockConfirm:")) {
                            String[] parts = response.split(":");
                            if (parts.length >= 4) {
                                String supplierId = parts[1];
                                String productId = parts[2];
                                String quantity = parts[3];
                                String notice = "商品 " + productId + " 已从供应商 " + supplierId + " 补货 " + quantity + " 件";
                                out.println("broadcastToEmployees:" + notice);
                            }
                        }
                        
                        System.out.print("请输入命令: ");
                    }
                } catch (IOException e) {
                    System.err.println("接收消息错误: " + e.getMessage());
                }
            });
            receiverThread.start();
            
            // 处理用户输入
            System.out.println("登录成功！可用命令：");
            System.out.println("1. 查看净利润: checkProfit");
            System.out.println("2. 查看会员数量: checkMembers");
            System.out.println("3. 广播通知员工: broadcast:消息内容");
            System.out.println("4. 退出: exit");
            
            String userInput;
            while ((userInput = stdIn.readLine()) != null) {
                if ("exit".equalsIgnoreCase(userInput)) {
                    out.println(userInput);
                    break;
                } else if ("checkProfit".equals(userInput)) {
                    // 查看净利润
                    BossDAO bossDAO = new BossDAO();
                    Boss boss = bossDAO.login(bossId, ""); // 仅查询，密码可为空
                    if (boss != null) {
                        System.out.println("当前净利润: " + boss.getNetProfit());
                    } else {
                        System.out.println("查询失败");
                    }
                } else if ("checkMembers".equals(userInput)) {
                    // 查看会员数量
                    BossDAO bossDAO = new BossDAO();
                    Boss boss = bossDAO.login(bossId, "");
                    if (boss != null) {
                        System.out.println("当前会员数量: " + boss.getMemberCount());
                    } else {
                        System.out.println("查询失败");
                    }
                } else if (userInput.startsWith("broadcast:")) {
                    // 广播消息给员工
                    String[] parts = userInput.split(":", 2);
                    if (parts.length == 2) {
                        out.println("broadcastToEmployees:" + parts[1]);
                    } else {
                        System.out.println("命令格式错误，正确格式: broadcast:消息内容");
                    }
                } else {
                    System.out.println("未知命令，请重新输入");
                }
                System.out.print("请输入命令: ");
            }
            
            receiverThread.interrupt();
            
        } catch (IOException e) {
            System.err.println("无法连接到服务器: " + e.getMessage());
        }
    }
    
    // 登录方法
    private static boolean login(BufferedReader stdIn) throws IOException {
        BossDAO bossDAO = new BossDAO();
        
        System.out.print("请输入您的老板ID: ");
        bossId = stdIn.readLine();
        
        System.out.print("请输入密码: ");
        String password = stdIn.readLine();
        
        Boss boss = bossDAO.login(bossId, password);
        return boss != null;
    }
}
    